#include "MsgWebView.h"
#include "MsgHtmlObj.h"
#include <QJsonObject>
#include <QjsonDocument>
#include "MsgWebPage.h"
#include <QWebChannel>
#include "MsgHtmlObj.h"
#include <QList>
#include <QDebug>
#include <qsqlquerymodel.h>


MsgWebView::MsgWebView(QWidget *parent)
	: QWebEngineView(parent),
	m_channel(new QWebChannel())
{
	MsgWebPage *myPage = new MsgWebPage(this);
	//QWebEnginePage *page = new QWebEnginePage(this);
	setPage(myPage);

	m_msgHtmlObj = new MsgHtmlObj(this);

	//QWebChannel *channel = new QWebChannel(this);
	m_channel->registerObject("external0", m_msgHtmlObj);

	QString strTalkID = WindowManager::getInstance()->getWindowId();
	QSqlQueryModel model;
	if (strTalkID.length() == 4)
	{
		if (strTalkID == "2000")
		{
			model.setQuery(
				QString("SELECT employeeID, picture FROM tab_employees WHERE status=1;")
			);
		}
		else
		{
			model.setQuery(
				QString("SELECT employeeID, picture FROM tab_employees WHERE status=1 AND departmentID=%1;")
				.arg(strTalkID)
			);
		}

		for (int i = 0; i < model.rowCount(); ++i)
		{
			QString strExterntl = "external_" + model.data(model.index(i, 0)).toString();
			QString strPicturePath = model.data(model.index(i, 1)).toString();

			MsgHtmlObj *obj = new MsgHtmlObj(this, strPicturePath);
			m_channel->registerObject(strExterntl, obj);
		}
		
	}
	else 
	{
		model.setQuery(
			QString("SELECT employeeID, picture FROM tab_employees WHERE status=1 AND employeeID=%1;")
			.arg(strTalkID)
		);

		QString strExterntl = "external_" + model.data(model.index(0, 0)).toString();
		QString strPicturePath = model.data(model.index(0, 1)).toString();

		MsgHtmlObj *obj = new MsgHtmlObj(this, strPicturePath);
		m_channel->registerObject(strExterntl, obj);
	}


	page()->setWebChannel(m_channel);
	load(QUrl("qrc:/Resources/MainWindow/MsgHtml/msgTmpl.html"));

	TalkWindowShell *talkWindowShell = WindowManager::getInstance()->m_talkwindowshell;
	connect(this, 
		SIGNAL(signalSendMsg(QString &, int, QString)),
		talkWindowShell, 
		SLOT(updateSendTcpMsg(QString &, int, QString))
	);
}

MsgWebView::~MsgWebView()
{
}

void MsgWebView::appendMsg(const QString & html, QString strObj)
{
	QJsonObject msgObj;
	QString qsMsg;
	QList<QStringList> msgList = parseHtml(html);

	int a = msgList.size();
	int b = msgList.count();

	QString strData;
	int msgType = 1;
	int imageNum = 0;

	for (int i = 0; i < msgList.count(); i++)
	{
		if (msgList.at(i).at(0) == "img")
		{
			QString imagePath = msgList.at(i).at(1);
			if (imagePath.left(3) == "qrc")
			{
				imagePath = imagePath.mid(3);
			}

			QPixmap pixmap(imagePath);
			//pixmap.load()

			/*QString imageHtml = QString::asprintf(
				"img src=\"qrc%s\" width=\"%d\" height=\"%d\"",
				imagePath, pixmap.width(), pixmap.height()
			);*/
			QString imageHtml = QString(
				"<img src=\"qrc%1\" width=\"%2\" height=\"%3\" />")
				
				.arg(imagePath).arg(pixmap.width()).arg(pixmap.height());
			
			QString imageNumber;
			imageNumber = imagePath.replace(":/Resources/MainWindow/emotion/", "");
			imageNumber = imagePath.replace(".png", "");
			while (imageNumber.size() < 3)
			{
				imageNumber.insert(0, "0");
			}

			imageNum++;
			msgType = 0;
			strData += imageNumber;

			qsMsg += imageHtml;
		}
		else if (msgList.at(i).at(0) == "text")
		{
			qsMsg += msgList.at(i).at(1);

			strData += msgList.at(i).at(1);
		}
	}

	msgObj.insert("MSG", qsMsg);
	const QString& msg = QJsonDocument(msgObj).toJson(QJsonDocument::JsonFormat::Compact);

	if (strObj == "0") 
	{
		this->page()->runJavaScript(QString("appendHtml0(%1)").arg(msg));

		if (msgType == 0)
		{
			strData = QString::number(imageNum) + "images" + strData;
		}

		//page()->runJavaScript(QString("appendHtml0(%1)").arg(msg));

		emit signalSendMsg(strData, msgType);
	}
	else
	{
		page()->runJavaScript(QString("recvHtml_%1(%2)").arg(strObj).arg(msg));

	}
	//page()->runJavaScript(QString("appendHtml(%1)").arg(msg));

	//signalSendMsg(strData, msgType);
}

void MsgWebView::setID(QString a)
{
	m_jID = a;
}

QList<QStringList> MsgWebView::parseHtml(const QString & html)
{
	QDomDocument dom;
	dom.setContent(html);

	QDomElement root = dom.documentElement();

	//const QDomNode &node = root.firstChildElement();
	const QDomNode &node = root.firstChildElement("body");

	return parseDomNode(node);
}

QList<QStringList> MsgWebView::parseDomNode(const QDomNode & node)
{	
	QList<QStringList> attribute;

	const QDomNodeList &list = node.childNodes();

	for (int i = 0; i < list.count(); i++)
	{
		const QDomNode &node = list.at(i);

		if (node.isElement())
		{
			const QDomElement &element = node.toElement();
			QStringList s;
			QString ss = element.tagName();
			if (element.tagName() == "img")
			{
				s << "img" << element.attribute("src");
				attribute << s;
			}
			else if (element.tagName() == "span")
			{
				s << "text" << element.text();
				attribute << s;
			}
			

			if (node.hasChildNodes())
			{	

				attribute << parseDomNode(node);
			}
		}
	}

	return attribute;
}
